package com.kedou.application;

import java.io.IOException;

import android.app.Application;
import android.os.Environment;
import android.widget.Toast;

import com.baidu.frontia.FrontiaApplication;
import com.kedou.app.Config;
import com.kedou.app.R;
import com.kedou.net.PostUpdateInfo;
import com.kedou.util.DebugUtil;
import com.kedou.util.DensityUtil;
import com.kedou.util.FileUtil;
import com.kedou.util.NetworkUtil;
import com.kedou.util.Util;

public class Myapplication extends Application {
	public static String DEBUG_PATH = "";
	/**
	 * 全局Token
	 */
	private String mToken;
	/**
	 * 手机唯一识别码 Imei
	 */
	private String mImei;
	/**
	 * 屏幕高度
	 */
	private int mDeviceHeight;
	/**
	 * 屏幕宽度
	 */
	private int mDeviceWidth;
	/**
	 * 根地址
	 */
	private String baseUrl;
	/**
	 * 序列号
	 */
	private String serial;

	public String getmImei() {
		return mImei;
	}

	public void setmImei(String mImei) {
		this.mImei = mImei;
	}

	public int getmDeviceHeight() {
		return mDeviceHeight;
	}

	public void setmDeviceHeight(int mDeviceHeight) {
		this.mDeviceHeight = mDeviceHeight;
	}

	public int getmDeviceWidth() {
		return mDeviceWidth;
	}

	public void setmDeviceWidth(int mDeviceWidth) {
		this.mDeviceWidth = mDeviceWidth;
	}

	public String getmToken() {
		return mToken;
	}

	public void setmToken(String mToken) {
		this.mToken = mToken;
	}

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		FrontiaApplication.initFrontiaApplication(getApplicationContext());
		if (!FileUtil.getInstance().isFileExist("", Config.FILENAME)) {
			try {
				FileUtil.getInstance().createFileInSDCard("", Config.FILENAME);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		DebugUtil.printE("sd前的serial", Util.getSDSerial());
		DebugUtil.printE("sd前的baseUrl", Util.getSDBaseUrl());
		getDeviceInfo();
		updateInfo();
	}

	/**
	 * 获取手机的信息
	 */
	private void getDeviceInfo() {
		setmDeviceWidth(DensityUtil.getScreenWidth(getApplicationContext()));
		setmDeviceHeight(DensityUtil.getScreenHeight(getApplicationContext()));
		setmImei(Util.getImei(getApplicationContext()));
		DebugUtil.printE("deviceid", getmImei());
	}

	/**
	 * 跟服务器交互，获取一些配置信息，比如baseUrl。。serial以及baseUrl都需要SD卡持久化
	 */
	private void updateInfo() {
		if (NetworkUtil.isNetworkAvailable(this)) {
			new PostUpdateInfo(Util.getSDSerial(),
					DensityUtil.getResolution(getApplicationContext()),
					Config.SOURCE, Util.getImei(getApplicationContext()),
					Util.getAppVersionName(getApplicationContext()),
					new PostUpdateInfo.SuccessCallback() {

						@Override
						public void onSuccess(String msg, String baseurl,
								String slogan, String serial) {
							DebugUtil.printE("网络中的serial", serial);
							DebugUtil.printE("网络中的baseUrl", baseurl);
							FileUtil.getInstance()
									.write2SD(
											"",
											Config.FILENAME,
											("serial=" + serial + ";baseUrl=" + baseurl)
													.getBytes());
							DebugUtil.printE("最终写入sd卡的内容", "serial=" + serial
									+ ";baseUrl=" + baseurl);
							DebugUtil.printE("sd后的serial", Util.getSDSerial());
							DebugUtil.printE("sd后的baseUrl", Util.getSDBaseUrl());
						}
					}, new PostUpdateInfo.FailCallback() {

						@Override
						public void onFail(String msg) {

						}
					}, getApplicationContext());
		} else {
			Toast.makeText(
					getApplicationContext(),
					getResources()
							.getString(R.string.please_connect_to_network),
					Toast.LENGTH_SHORT).show();
		}

	}

}
